<template>
  <transition name="container">
    <div v-if="visible" @click.stop="" class="homemenu-box" @contextmenu.stop="">
      <div class="search-box">
        <el-input v-model="searchValue" clearable placeholder="搜索应用" @change='searchApp'>
          <template #prefix>
              <el-icon class="el-input__icon"><search /></el-icon>
          </template>
        </el-input>
      </div>
      <div class="flex-between">
        <p class="menu-t">菜单</p>
        <div class="all-app pointer" @click="isAll=true" v-if="!isAll">
          所有应用
          <el-icon style="margin-left:5px"><arrow-right /></el-icon>
        </div>
        <div class="all-app pointer" @click="isAll=false" v-else>
          <el-icon style="margin-right:5px"><arrow-left /></el-icon>
          返回
        </div>
      </div>

      <div class="app-box" v-if="!isAll">
        <div
        class="inline-icon"
        v-for="item in queryAppList.length<=0?app.slice(0,16):queryAppList"
        :key="item.id"
        @click="handleOpen(item)"
        :appId='item.id'>
            <div>
                <p>
                    <img
                    draggable="false"
                    :src="item.icon" 
                    :alt="item.name">
                </p>
                <p>{{item.name}}</p>
            </div>
        </div>
      </div>

      <div class="app-all-box scrollbar" v-else>
        <div
        class="inline-icon"
        v-for="item in queryAppList.length<=0?app:queryAppList"
        :key="item.id"
        @click="handleOpen(item)"
        :appId='item.id'>
            <div>
                <p>
                    <img
                    draggable="false"
                    :src="item.icon" 
                    :alt="item.name">
                </p>
                <p>{{item.name}}</p>
            </div>
        </div>
      </div>

      <div class="user-box">
        <div class="flex-between">
          <div class="flex-center user-info pointer">
            <el-avatar 
              src='https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png' 
              :size='40'>
            </el-avatar>
            <p style="margin-left:10px">{{cache.username.get()}}</p>
          </div>
          <el-popconfirm 
             title="是否要退出登录？"
             @confirm="logon"
             @cancel.stop=''
             confirm-button-text="确定"
             cancel-button-text="取消"
             popper-class='z-popover'
             placement='top'
             >
              <template #reference>
                <div class="switch pointer flex-center">
                  <el-icon :size="20"><switch-button /></el-icon>
                </div>
              </template>
          </el-popconfirm>
          
        </div>
      </div>

    </div>
  </transition>
</template>

<script>
import cache from '@/utils/cache'
import {getCurrentInstance,ref,reactive } from 'vue'
export default {
  name: 'HomeMenu',
  components: {},
  props: {
    visible:{
      type:Boolean,
      default:false
    },
    app:{
      type:Array,
      default:[]
    }
  },
  emits: ['open','close'],
  setup(props,context) {
    const {proxy} = getCurrentInstance();
    let searchValue = ref('')
    let isAll = ref(false)
    let queryAppList = reactive([])

    function handleOpen(item){
      context.emit('open',item)
      context.emit('close')
    }

    function logon(){
      proxy.$utils.loginOut()
    }

    function searchApp(){
      if(searchValue.value.trim()===''){
        queryAppList.length=0
        return
      }
      let reg =  new RegExp(searchValue.value,'i');
      props.app.forEach(item => {
        if(reg.test(item.name)){
          queryAppList.push(item)
        }
      });
    }
    return {
      handleOpen,
      searchApp,
      cache,
      logon,
      searchValue,
      queryAppList,
      isAll
    }
  }
} 

</script>
<style lang='scss' scoped>
@keyframes translateAnimationShow {
    from 
    {
      opacity: 0;
      transform: translateX(-50%) scaleY(0);
    }
    to 
    {
      bottom: 10px;
      opacity: 1;
      transform: translateX(-50%) scaleY(1);

    }
}
  
@keyframes translateAnimationHide {
    from 
    {
      opacity: 1;
      transform: translateX(-50%) scaleY(1);
    }
    to 
    {
      opacity: 0;
      transform: translateX(-50%) scaleY(0);
    }
}
.homemenu-box {
  z-index: 2100;
  width: 600px;
  height: 600px;
  background-color: rgba(255,255,255, .9);
  position: absolute;
  left: 50%;
  bottom: 10px;
  border-radius: 10px;
  padding: 40px 40px 10px;
  transform: translateX(-50%);
  transform-origin: bottom;
  overflow: hidden;
  box-shadow: 0px 0px 2px 1px #efefef;
}

.container-enter-active {
  animation-name: translateAnimationShow;
  animation-duration: .2s;
  animation-fill-mode: forwards;
}

.container-leave-active {
  animation-name: translateAnimationHide;
  animation-duration: .2s;
  animation-fill-mode: forwards;
}

.search-box {
  margin-bottom: 30px;
  margin-left: 30px;
  margin-right: 34px;
}

.menu-t {
  font-weight: 600;
  color: #7c7b7b;
  margin-left: 31px;
}

.all-app {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 5px 8px;
  border-radius: 5px;
  background-color: $white;
  font-size: 13px;
  border: 1px solid #efefef;
  margin-right: 34px;
}

.app-box {
  margin-top: 10px;
  display: grid;
  grid-template-columns:repeat(5,1fr);
  grid-template-rows: repeat(3,1fr);

}

.app-all-box {
  margin-top: 10px;
  display: grid;
  grid-template-columns:repeat(5,1fr);
  grid-template-rows: repeat(3,1fr);
  height: 300px;
  max-height: 300px;
  overflow-y: auto;
}

.inline-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 80px;
    max-width: 80px;
    padding: 5px;
    margin: 10px;
    cursor: pointer;
    background-color: transparent;
    border-radius: 5px;

    div {
        text-align: center;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;

        p {
            font-size: 13px;
            color: $PrimaryTextColor;
            @include ellipsis(2);
            width: 80px;
            padding: 5px 5px;

            img {
                height: 40px;
                width: 40px;
            }
        }
    }

    &:hover {
        background-color: rgba(255,255,255, .4);
    }

    &:active {
        background-color: rgba(255,255,255, .6);
    }
}

.user-box {
  position: absolute;
  bottom: 0;
  left: 0;
  padding: 20px 40px;
  width: 100%;
  background-color: rgba(255,255,255,.8);

  .user-info {
    flex-direction: row;
  }

  .switch {
    border-radius: 80px;
    width: 40px;
    height: 40px;
    
    &:hover {
      background-color: rgba(226, 226, 226, 0.8);
    }
  }
}
</style>
